package dao;

import model.RegRecord;
import util.DbUtil;

import java.math.BigDecimal;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class RegRecordDao {
    public static boolean insert(RegRecord model)
    {
        boolean flag=false;
        try
        {
            Connection conn= DbUtil.getConnection();
            Timestamp timestamp = new Timestamp(model.getRegTime().getTime());
            String sql="insert into reg_record(patientId,deptId,regTime,price)";
            sql +="values(?,?,?,?);";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, model.getPatientId());
            pstmt.setInt(2, model.getDeptId());
            pstmt.setString(3,String.valueOf(timestamp));
            pstmt.setBigDecimal(4, model.getPrice());
            //pstmt.executeUpdate();
            if (pstmt.executeUpdate() > 0)
            {        // 至少已经更新了一行
                flag = true;
            }
            DbUtil.release(pstmt, conn);
        } catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    public static void update(RegRecord model)
    {
        try
        {
            Connection conn=DbUtil.getConnection();
            String sql="update reg_record set patientId = ?, deptId = ?, regTime =current_timestamp(), price = ?";
            sql +=" where id = ?;";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, model.getPatientId());
            pstmt.setInt(2, model.getDeptId());
            pstmt.setBigDecimal(3, model.getPrice());
            pstmt.setInt(4, model.getId());
            pstmt.executeUpdate();
            DbUtil.release(pstmt, conn);
        } catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void delete(Integer id)
    {
        try
        {
            Connection conn=DbUtil.getConnection();
            String sql="delete from reg_record where id = ?;";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            pstmt.executeUpdate();
            DbUtil.release(pstmt, conn);
        } catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<RegRecord> select(Integer id, Integer patientId, Integer deptId, Date beginTime, Date endTime, BigDecimal price)
    {
        try
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String sql="select r.id as id, r.patientId as patient_id, r.deptId as dept_id, "
                    + "r.regTime as reg_time, r.price as price, "
                    + "p.name as patient_name, d.name as dept_name "
                    + "from reg_record r "
                    + "left join patient p on r.patientId = p.id "
                    + "left join dept d on r.deptId = d.id ";
            sql += "where 1=1 ";
            if(id != null)
            {
                sql += "and r.id = '"+id+"' ";
            }
            if(patientId != null)
            {
                sql += "and p.id = '"+patientId+"' ";
            }
            if(deptId != null)
            {
                sql += "and d.id = '"+deptId+"' ";
            }
            if(beginTime != null)
            {
                sql += "and r.regTime >= '"+dateFormat.format(beginTime)+"' ";
            }
            if(endTime != null)
            {
                sql += "and r.regTime < '"+dateFormat.format(endTime)+"' ";
            }
            if(price != null)
            {
                sql += "and r.price = '"+price+"' ";
            }
            sql +=";";
            return fillResult(sql);
        } catch (ClassNotFoundException | SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return new LinkedList<>();
        }
    }

    private static List<RegRecord> fillResult(String sql) throws ClassNotFoundException, SQLException
    {
        List<RegRecord> regRecordList = new LinkedList<>();
        Connection conn = DbUtil.getConnection();
        PreparedStatement pstmt=conn.prepareStatement(sql);
        ResultSet rs= pstmt.executeQuery();
        while(rs.next())
        {
            RegRecord regRecord = new RegRecord(rs.getInt("id"),rs.getInt("patient_id"),
                    rs.getInt("dept_id"),rs.getTimestamp("reg_time"),rs.getBigDecimal("price"),
                    rs.getString("dept_name"),rs.getString("patient_name"));
            regRecordList.add(regRecord);
        }
        DbUtil.release(pstmt, conn);
        return regRecordList;
    }
}
